#!/bin/sh

if test "$CONFIGURE " != "YES "
then
	echo
	echo Error: Wrong usage
	echo
	echo You must run `dirname $0`/configure instead of $0
	exit 1
fi

if test "`ls .` " != " "
then
	echo Error: Current directory must be empty
	exit 1
fi

if test "`uname -p`" = "sparc"
then
KERNEL32=sparc
KERNEL64=sparcv9
KERNEL32FLAGS=-U
KERNEL64FLAGS=-K
else
if test "`uname -r`" = "5.9"
then
KERNEL32=i386
KERNEL64=NULL
KERNEL32FLAGS=
KERNEL64FLAGS=
else
KERNEL32=i386
KERNEL64=amd64
KERNEL32FLAGS=
KERNEL64FLAGS=-K
fi
fi

ln -s $SRCDIR/setup/SunOS/build.sh build.sh
ln -s $SRCDIR origdir
ln -s $SRCDIR/misc/samples/ddksample .
echo SRCDIR=$SRCDIR>.directories

# Make the 32bit kernel drivers
mkdir $KERNEL32
echo "cd $KERNEL32;sh $SRCDIR/setup/setupdir.sh -A$KERNEL32 $KERNEL32FLAGS"
(cd $KERNEL32;sh $SRCDIR/setup/setupdir.sh -A$KERNEL32 $KERNEL32FLAGS)

# If KERNEL64 is specified then do both 32 and 64bit
if test "$KERNEL64" != "NULL"
then
	mkdir $KERNEL64
	echo "cd $KERNEL64;sh $SRCDIR/setup/setupdir.sh -A$KERNEL64 $KERNEL64FLAGS"
	(cd $KERNEL64;sh $SRCDIR/setup/setupdir.sh -A$KERNEL64 $KERNEL64FLAGS)

	# Make sure both 32 and 64 bit versions share the same timestamp.h file.
	cp $KERNEL32/kernel/framework/include/timestamp.h $KERNEL64/kernel/framework/include/timestamp.h 

	cat > Makefile <<THE_END_IS_NEAR
# Autogenerated file - do not edit

all:	$KERNEL32/kernel/framework/include/buildid.h subdirs build

install:	all
	rm -f prototype/kernel/drv/*.conf
	cp -R prototype/* /
	sync
	@echo You need to reboot or run ossdetect to reload the drivers

subdirs:
	cd $KERNEL32;make
	cd $KERNEL64;make

build:
	sh build.sh

copy:	all
	rm -f prototype/kernel/drv/*.conf
	cp -R prototype/* /

package:	all
	sh $KERNEL32/setup/SunOS/mkpkg.sh

clean:
	cd $KERNEL32;make clean
	cd $KERNEL64;make clean
	rm -f *.pkg
	rm -rf prototype

config:
	cd $KERNEL32;make config CONFIG_FLAGS=-A$KERNEL32
	cd $KERNEL64;make config CONFIG_FLAGS="-A$KERNEL64 -K"

THE_END_IS_NEAR

else	# Do just the 32bit kernel

cat > Makefile <<THE_END_IS_NEAR
# Autogenerated file - do not edit

all:    $KERNEL32/kernel/framework/include/buildid.h subdirs build

install:        all
	rm -f prototype/kernel/drv/*.conf
	cp -R prototype/* /
	sync
	@echo You need to reboot or run ossdetect to reload the drivers

subdirs:
	cd $KERNEL32;make

build:
	sh build.sh

copy:   all
	rm -f prototype/kernel/drv/*.conf
	cp -R prototype/* /

package:        all
	sh $KERNEL32/setup/SunOS/mkpkg.sh

clean:
	cd $KERNEL32;make clean
	rm -f *.pkg
	rm -rf prototype

config:
	cd $KERNEL32;make config CONFIG_FLAGS=-A$KERNEL32

THE_END_IS_NEAR
fi

exit 0
